﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SchemaGraph.Interfaces;
using SchemaGraph.Interfaces.Vertexes;
using SchemaGraph.Interfaces.Edges;
using GraphBase.Core;
using ObjectGraph.Core;


namespace SchemaGraph.Vertexes
{
    public class DatatypeVertex : GraphBase.Vertex, IDatatypeVertex
    {
        protected string m_name;
        public DatatypeVertex(string name)
        {
            m_name = name;
        }

        #region IDatatypeVertex Members

        public string Name
        {
            get { return m_name; }
        }

        public bool IsDerivedFrom(IDatatypeVertex parent)
        {
            return Operations.RunBreadthFirstQuery(new GraphBase.Interfaces.IVertex[] { this }
                , null
                , null
                , delegate(GraphBase.Interfaces.IEdge edge)
                {
                    IDerivationEdge derivationEdge = edge as IDerivationEdge;
                    if (Object.Equals(derivationEdge.IncomingVertex, parent))
                        return true;
                    return false;
                }
                , delegate(GraphBase.Interfaces.IEdge edge) { return edge is IDerivationEdge; }
                , true);
        }

        public bool IsListType()
        {
            return Operations.RunDepthFirstQuery(this
                , null
                , null
                , delegate(GraphBase.Interfaces.IEdge edge) { return edge is IListDerivationEdge; }
                , delegate(GraphBase.Interfaces.IEdge edge) { return edge is IDerivationEdge; }
                , true);
        }

        /// <summary>
        /// Get parent edges
        /// </summary>
        /// <returns>Returns a translating enumerable composed of a filtering enumerable</returns>
        public IEnumerable<IDerivationEdge> GetParentEdges()
        {
            return new TranslatingEnumerable<GraphBase.Edge, IDerivationEdge>(
                delegate(GraphBase.Edge edge) { return edge as IDerivationEdge; }
                , new FilteringEnumerable<GraphBase.Edge>(zzIncomingEdges
                    , delegate(GraphBase.Edge edge) { return edge is IDerivationEdge; }));
               
        }

        /// <summary>
        /// Get child edges
        /// </summary>
        /// <returns>Returns a translating enumerable of a filtering enumerable</returns>
        public IEnumerable<IDerivationEdge> GetChildEdges()
        {
            return new TranslatingEnumerable<GraphBase.Edge, IDerivationEdge>(
                delegate(GraphBase.Edge edge) { return edge as IDerivationEdge; }
                , new FilteringEnumerable<GraphBase.Edge>(zzOutgoingEdges
                    , delegate(GraphBase.Edge edge) { return edge is IDerivationEdge; }));
        }

        #endregion
    }
}
